import { getToken } from './deepin-auth'

const GATEWAY_URL = 'https://pay.deepin.org/v1'

export const createPayment = async (params) => {
  try {
    const response = await fetch(`${GATEWAY_URL}/transactions`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'X-Auth-Token': getToken()
      },
      body: JSON.stringify({
        amount: params.amount * 100, // 单位: 分
        currency: 'CNY',
        payment_method: params.method,
        order_id: params.submissionId
      })
    })

    const { data: paymentInfo } = await response.json()
    
    return {
      qrcodeUrl: paymentInfo.qrcode_url,
      transactionId: paymentInfo.id
    }
  } catch (error) {
    console.error('支付请求失败:', error)
    throw new Error('支付系统暂时不可用')
  }
}

export const checkPaymentStatus = async (transactionId) => {
  const response = await fetch(`${GATEWAY_URL}/transactions/${transactionId}`)
  const { status } = await response.json()
  return status === 'success'
}